#include "tcpClient.h"
#include <QGuiApplication>
#include <QIcon>
#include <QQmlApplicationEngine>
#include <QQuickView>

QObject* SingletonTCPClientController(QQmlEngine*, QJSEngine*)
{
    return new TCPClientController();
}

int main(int argc, char* argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    // init setting file path
    QString applicationName = QStringLiteral("GatewayTools");
    QString orgName = QStringLiteral("wml");
    QString orgDomain = QStringLiteral("gateway");
    app.setApplicationName(applicationName);
    app.setOrganizationName(orgName);
    app.setOrganizationDomain(orgDomain);
    QSettings::setDefaultFormat(QSettings::IniFormat);
    // init app icon
    app.setWindowIcon(QIcon(":/images/gateway.png"));
    // connect backend and frontend
    qmlRegisterSingletonType<TCPClientController>("tcpClient", 1, 0, "TCPClientController", SingletonTCPClientController);
    // qt
    QQmlApplicationEngine engine;
    engine.addImportPath(".");
    const QUrl url(QStringLiteral("qrc:/qml/main.qml"));
    QObject::connect(
        &engine, &QQmlApplicationEngine::objectCreated,
        &app, [url](QObject* obj, const QUrl& objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1); },
        Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}
